package com.mstore.action;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mstore.pojo.Account;
import com.mstore.pojo.BgiveRule;
import com.mstore.service.BgiveRuleService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 赠送积分规则
 */
public class BgiveRuleAction extends BaseAction {

	private static final long serialVersionUID = 7303019118460149301L;

	private BgiveRuleService bGiveRuleService;

	private BgiveRule bGiveRule = new BgiveRule();

	private Map<String, Object> session = ActionContext.getContext()
			.getSession();

	private String result;

	private List<BgiveRule> bGiveRules = new ArrayList<BgiveRule>();

	private String id;

	private String times;

	private double regScore;

	private String message;

	private String bFromTime;

	private String bToTime;

	private String bId;

	/**
	 * 查看积分规则
	 */
	public String watchBgiveRule() {
		Object object = session.get("account");
		if (object == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		Account account = (Account) object;
		BgiveRule bgiveRule = new BgiveRule();
		bgiveRule.setEaId(account.getId());
		bGiveRules = bGiveRuleService.watchBgiveRule(bgiveRule);
		return "watchBgiveRuleSuccess";
	}

	/**
	 * 添加积分规则
	 */
	public String saveBgiveRule() throws ParseException {
		Object object = session.get("account");
		Account account = (Account) object;
		BgiveRule rule = new BgiveRule();
		rule.setEaId(account.getId());
		Date date = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
		Date dateNow = df1.parse(new SimpleDateFormat("yyyy-MM-dd").format(date));
		bGiveRule.setCreateTime(df.format(new Date()));
		bGiveRule.setEaId(account.getId());
		bGiveRules = bGiveRuleService.watchBgiveRule(rule);
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date fromTime = fmt.parse(bGiveRule.getFromTime());
		Date toTime = fmt.parse(bGiveRule.getToTime());
		if (fromTime.after(toTime) || toTime.before(fromTime)) {
			message = "起止时间填写有误";
			return "saveDateTimeError";
		}
		if (fromTime.before(dateNow) || toTime.before(dateNow)) {
			message = "填写时间小于当前时间";
			return "saveTimeError";
		}

		if (bGiveRules.size() != 0) {
			for (int i = 0; i < bGiveRules.size(); i++) {
				Date bFromTime = fmt.parse(bGiveRules.get(i).getFromTime());
				Date bToTime = fmt.parse(bGiveRules.get(i).getToTime());
				if (fromTime.after(bFromTime) && toTime.before(bToTime)) {
					message = "活动时间已存在";
					return "saveBgiveRuleError";
				}
				// 当时间包含某个时间
				if (fromTime.before(bFromTime) && toTime.after(bToTime)) {
					message = "活动时间已存在";
					return "saveBgiveRuleError";
					// 当开始时间在某个时间范围内
				}
				if (fromTime.after(bFromTime) && fromTime.before(bToTime)) {
					message = "活动时间已存在";
					return "saveBgiveRuleError";
					// 当结束时间在某个时间范围内
				}
				if (toTime.before(bToTime) && toTime.after(bFromTime)) {
					message = "活动时间已存在";
					return "saveBgiveRuleError";
				}

			}
		}
		bGiveRuleService.saveBgiveRule(bGiveRule);
		return "saveBgiveRuleSuccess";
	}

	/**
	 * * 通过类型查询赠送积分规则
	 */
	public String entrygiveValue() {
		String temp = "error";
		Account account = (Account) session.get("account");
		bGiveRule = new BgiveRule();
		if (account != null) {
			bGiveRule.setEaId(account.getId());
			bGiveRule.setType(1);
			bGiveRule = bGiveRuleService.findBgiveRuleByType(bGiveRule);
			temp = "entry";
		}
		return temp;
	}

	/**
	 * 设置注册时积分
	 */
	public String setgiveValue() {
		Account account = (Account) session.get("account");
		bGiveRule = new BgiveRule();
		bGiveRule.setEaId(account.getId());
		bGiveRule.setType(1);
		bGiveRule = bGiveRuleService.findBgiveRuleByType(bGiveRule);
		if (bGiveRule == null) {
			bGiveRule = new BgiveRule();
			bGiveRule.setCreateTime(getCreateTime());
			bGiveRule.setGiveValue(regScore);
			bGiveRule.setEaId(account.getId());
			bGiveRule.setType(1);
			bGiveRule.setState(1);
			bGiveRuleService.saveBgiveRule(bGiveRule);
		}
		else {
			bGiveRule.setGiveValue(regScore);
			bGiveRuleService.modifyGiveValue(bGiveRule);
		}
		return "setSuccess";
	}

	/**
	 * 查询某个积分规则
	 */
	public String getOneBgiveRule() {
		BgiveRule bgiveRule = new BgiveRule();
		bgiveRule.setId(Long.parseLong(id));
		bGiveRule = bGiveRuleService.getOneBgiveRule(bgiveRule);
		return "getOneBgiveRuleSuccess";
	}

	/**
	 * 删除某个积分规则
	 */
	public String delBgiveRule() {
		BgiveRule bgiveRule = new BgiveRule();
		bgiveRule.setId(Long.parseLong(id));
		bGiveRuleService.delBgiveRule(bgiveRule);
		return "delBgiveRuleSuccess";
	}

	/**
	 * 修改某个积分规则
	 */
	public String modifyBgiveRule() {
		Object object = session.get("account");
		Account account = (Account) object;
		bGiveRule.setEaId(account.getId());
		bGiveRuleService.modifyBgiveRule(bGiveRule);
		return "modifyBgiveRulesSuccess";
	}

	/**
	 * 检测时间范围的合理性
	 */
	public String checkTime() throws ParseException {
		Account account = (Account) session.get("account");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		BgiveRule rule = new BgiveRule();
		rule.setEaId(account.getId());
		Date date = new Date();
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date fromTime = fmt.parse(bFromTime);
		Date toTime = fmt.parse(bToTime);
		Date dateNow = df.parse(df.format(date));
		bGiveRules = bGiveRuleService.watchBgiveRule(rule);
		Map<String, Object> map = new HashMap<String, Object>();
		if (fromTime.after(toTime) || toTime.before(fromTime)) {
			map.put("temp", "当前时间填写有误");
			result = super.mapTojson(map);
			return "updateDateTimeError";
		}
		if (fromTime.before(dateNow) || toTime.before(dateNow)) {
			map.put("temp", "填写时间小于当前时间");
			result = super.mapTojson(map);
			return "updateTimeError";
		}
		if (bGiveRules.size() != 0) {
			for (int i = 0; i < bGiveRules.size(); i++) {
				if (bGiveRules.get(i).getId() != Long.parseLong(bId)) {
					Date bFromTime = fmt.parse(bGiveRules.get(i).getFromTime());
					Date bToTime = fmt.parse(bGiveRules.get(i).getToTime());
					// 当时间在某个时间范围内
					if (fromTime.after(bFromTime) && toTime.before(bToTime)) {
						map.put("temp", "活动时间已存在");
						result = super.mapTojson(map);
						return "modifyBgiveRulesError";
					}
					// 当时间包含某个时间
					if (fromTime.before(bFromTime) && toTime.after(bToTime)) {
						map.put("temp", "活动时间已存在");
						result = super.mapTojson(map);
						return "modifyBgiveRulesError";
						// 当开始时间在某个时间范围内
					}
					if (fromTime.after(bFromTime) && fromTime.before(bToTime)) {
						map.put("temp", "活动时间已存在");
						result = super.mapTojson(map);
						return "modifyBgiveRulesError";
						// 当结束时间在某个时间范围内
					}
					if (toTime.before(bToTime) && toTime.after(bFromTime)) {
						map.put("temp", "活动时间已存在");
						result = super.mapTojson(map);
						return "modifyBgiveRulesError";
					}
				}
			}
		}

		map.put("temp", "");
		result = super.mapTojson(map);
		return "modifyRuleSuccess";
	}

	public BgiveRuleService getbGiveRuleService() {
		return bGiveRuleService;
	}

	public void setbGiveRuleService(BgiveRuleService bGiveRuleService) {
		this.bGiveRuleService = bGiveRuleService;
	}

	public BgiveRule getbGiveRule() {
		return bGiveRule;
	}

	public void setbGiveRule(BgiveRule bGiveRule) {
		this.bGiveRule = bGiveRule;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public List<BgiveRule> getbGiveRules() {
		return bGiveRules;
	}

	public void setbGiveRules(List<BgiveRule> bGiveRules) {
		this.bGiveRules = bGiveRules;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTimes() {
		return times;
	}

	public void setTimes(String times) {
		this.times = times;
	}

	public double getRegScore() {
		return regScore;
	}

	public void setRegScore(double regScore) {
		this.regScore = regScore;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getbFromTime() {
		return bFromTime;
	}

	public void setbFromTime(String bFromTime) {
		this.bFromTime = bFromTime;
	}

	public String getbToTime() {
		return bToTime;
	}

	public void setbToTime(String bToTime) {
		this.bToTime = bToTime;
	}

	public String getbId() {
		return bId;
	}

	public void setbId(String bId) {
		this.bId = bId;
	}

}
